<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;

class OrderForm extends Model{
    protected $table;
    protected $_db_select = 'mysql_bgac';
    const DB_FIELDS = 'area,udid,account_id,player_id,level';

    public function paginate()
    {
        $perPage = Request::get('per_page', 20);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $tableAccount = 'platsdk_order';
        $rw = \DB::connection('mysql_bgac')->table($tableAccount);
        self::SourceCondition($rw);
        $total = $rw->get()->count();
        $rw = $rw->skip($start)->take($perPage);
        $data = $rw->get();
        $data = $data->toArray();
        for ($i = 0; $i < count($data); $i++){
            if ($data[$i]->orderSerial != null){
                $data[$i]->OrderStatus = null;
            }
        }
        // $data = json_decode($data, true);
        \Log::debug(__METHOD__, [$rw->toSql()]);
        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }
    // 获取玩家账号id
    public static function getAccountByName($name = "", $unsetFld = ''){
        if (!$name) {
            !$unsetFld && $unsetFld = 'area';
            $name = Input::get($unsetFld);
            if (!$name){
                return;
            }
        }
        $op = DB::connection('mysql_bgsi')->table('user_logout');
        $list = $op->get()->map(function($obj){return (array)$obj;})->toArray();

        $account = array_column($list, 'account') ?: [];

        //$arr = Input::all();
        //$arr['account'] = $account;
        //$arr[$unsetFld] = NULL;
        //Input::replace($arr);
        //var_dump(Input::all());exit;
        return $account ?: [];
    }

    //删除订单
//    public static function del($id){
//        \DB::connection('mysql_bgsi')->table('user_logout')->where('id', $id)->delete();
//    }

    //修改订单
//    public static function editsave($id,$data){
//        \DB::connection('mysql_bgsi')->table('user_logout')->where('id', $id)->update($data);
//    }

    public static function SourceCondition(&$rw)
    {
        $cond = Request::all();
        if ($cond['cp_player_id']){
            $rw->where("cp_player_id", 'like', $cond["cp_player_id"].'%');
        }
        if ($cond["cp_player_id"]){
            $rw->where("cp_player_id", $cond['cp_player_id'] ?: $cond['cp_player_id']);
        }

    }




    public static function with($relations)
    {
        return new static;
    }

    public function selectdb($select = ''){
        if ($select) {
            return $this->_db_select = $select;
        }
        return $this->_db_select;
    }

    public function getConnectionName(){
        return $this->selectdb();
    }
    public function getTable($table = ''){
        if ( !$table ){
            return 'user_logout';
        }
        if ($table) {

            $this->table = $table;
        }

        return $this->table;
    }
    public static function detailPlayer($accountId){

        $tableAccount = 'platsdk_order';
        $data = \DB::connection('mysql_bgac')->table($tableAccount)->where('id',$accountId)->first();

        $data = (array)$data;
        //$data['playerList'] = json_decode($data['playerList'], true)?:[];
        $data['playerList'] = DB::connection('mysql_bgsi')->table('player_info')->where('account', $accountId)->get(['playerId', 'area as areaId', 'name as playerName','level', 'diamond'])->map(function($obj){return (array)$obj;})->toArray();

        $data['area_info'] = \App\Models\Area::areaAll();
        $data['area_info'] = array_column($data['area_info'], 'name', 'areaId');

        $style = $data['is_seal_number'] == 1 ? 'success':'default';
        $alias = $data['is_seal_number'] == 1 ? 'On':'Off';
        $data['seal_alias'] = '<span class="label label-'.$style.'">'.$alias.'</span>';

        $area = \Request::get("area") ?: $data['lastAreaId'];
        $rtn_info = [];

        \App\Models\SendSocketToGame::SendAction(function($socket)use(&$rtn_info, $data, $area){
            $msg['msgid'] = \App\Service\MessageDef::http_plat_game_request;
            $msg["area"] = \App\Models\Area::getPhysicsArea($area);
            $msg['game_msgid'] = \App\Service\MessageDef::g2p_player_info_req;
            $msg["playerId"] = 1;
            $msg["game_msg"] = [
                'account' => $data['account'],
            ];
            $ret = $socket->send($msg);
            \Log::debug('send single player mail', []);

            $rtn_info = $socket->ReadHttpReponse();
            //\Log::debug('read mail reponse data', [$msg,$rtn_info]);
        });

        $data['player_info'] = $rtn_info['data'];
        \Log::Debug(__METHOD__, [$rtn_info, $data]);
        return $data;
    }

}
